package com.chilli.cosima.data.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.chilli.cosima.utils.SpringUtils;

public class DBManager
{
	private static DBManager instance;

	private static DataSource dataSource;

	private DBManager()
	{
		dataSource = (DataSource) SpringUtils.getBean("dataSource");
	}

	public static DBManager getInstance()
	{
		if (instance == null)
		{
			synchronized (DBManager.class)
			{
				if (instance == null)
				{
					instance = new DBManager();
				}

			}

		}
		return instance;
	}

	public Connection getConnection() throws SQLException
	{
		Connection con = dataSource.getConnection();

		while (con.isClosed())
		{
			con = dataSource.getConnection();
		}

		return con;
	}

	public void closeConnection(Connection dbConnection) throws SQLException
	{
		if (dbConnection == null || dbConnection.isClosed())
		{
			return;
		}
		dbConnection.close();
	}
}
